var iRunner = window.iRunner || (window.iRunner = {});

// 过滤空值
iRunner.filter = function(obj) {
    return obj ? obj : '-'
}

// 报名选手审核状态转换
iRunner.registerExamineStatus = function(value) {
    if (value == 'normal') {
        return '成功'
    } else if (value == 'verify') {
        return '待审核'
    } else if (value == 'refuse') {
        return '不通过'
    } else if (value == 'cancel') {
        return '已取消'
    } else {
        return '-'
    }
}

// 报名选手支付状态转换
iRunner.registerPayStatus = function(value) {
    if (value == 'paid') {
        return '已支付';
    } else if (value == 'unpaid') {
        return '未支付';
    } else if (value == 'cancel') {
        return '已取消';
    } else if (value == 'no_need') {
        return '-';
    } else {
        return '-';
    }

}
// 报名选手支付状态按钮转换
iRunner.registerPayBtnStatus = function(row) {
    if (row.status == 'cancel') {
        return '-';
    } else if (row.pay_status == 'paid') {
        return "<button type='button' class='btn btn-sm btn-primary m-t-n-xs' style='margin-top: auto;' data-action='pay' data-registerid='" + row.register_id + "' data-name='" + row.passport_name + "' data-paystatus='" + row.pay_status + "' >设为未支付</button>";
    } else if (row.pay_status == 'unpaid') {
        return "<button type='button' class='btn btn-sm btn-primary m-t-n-xs' style='margin-top: auto;' data-action='pay' data-registerid='" + row.register_id + "' data-name='" + row.passport_name + "' data-paystatus='" + row.pay_status + "' >设为已支付</button>";
    } else if (row.pay_status == 'no_need') {
        return '-';
    } else {
        return '-';
    }

}
// 审核按钮
iRunner.registerExamineBtnStatus = function(row) {
    if (row.status == 'normal') {
        //成功的场合，审核不通过按钮
        return "<button type='button' class='btn btn-sm btn-primary m-t-n-xs' style='margin-top: auto;' data-action='examine' data-registerid='" + row.register_id + "' data-name='" + row.passport_name + "' data-status='" + row.status + "' data-registerid='" + row.register_id + "' >审核不通过</button>";
    } else if (row.status == 'verify') {
        //待审核的场合 两个按钮
        return "<button type='button' class='btn btn-sm btn-primary m-t-n-xs' style='margin-top: auto;' data-action='examine' data-registerid='" + row.register_id + "' data-name='" + row.passport_name + "' data-status='" + row.status + "' data-registerid='" + row.register_id + "' >审核通过</button>" + "&nbsp;<button type='button' class='btn btn-sm btn-primary m-t-n-xs' style='margin-top: auto;' data-action='examine' data-registerid='" + row.register_id + "' data-name='" + row.passport_name + "' data-status='" + row.status + "' data-registerid='" + row.register_id + "' >审核不通过</button>";
    } else if (row.status == 'refuse') {
        // 审核不通过的场合 通过按钮
        return "<button type='button' class='btn btn-sm btn-primary m-t-n-xs' style='margin-top: auto;' data-action='examine' data-registerid='" + row.register_id + "' data-name='" + row.passport_name + "' data-status='" + row.status + "' data-registerid='" + row.register_id + "' >审核通过</button>";
    } else {
        return '-';
    }

}

// 性别
iRunner.gender = function(gender) {
    return gender ? (gender == 'unknown' ? '未知' : (gender == 'male' ? '男' : '女')) : '未知'
}

// 初始化接口基础路径
iRunner.config = {
    baseUrl: 'http://' + document.domain + '/dashboard'
    // baseUrl: 'http://timing.runningtogether.net/dashboard'
}

// CP点类型
iRunner.cpTypeConfig = [
    {
        type: 'middle',
        name: '中间点' 
    },
    {
        type: 'start',
        name: '起点' 
    },
    {
        type: 'retrace',
        name: '折返点' 
    }
];

// 初始化赛事规模基本信息
iRunner.raceFrequencyConfig = {
    number: ['3场以内', '3-10场', '10场以上'],
    scale: ['500人以下', '500-1000人', '1000-3000人', '3000人以上']
}

// 团队性别
iRunner.teamGender = [
    {
        id: 'mixed',
        text: '混合'
    },
    {
        id: 'female',
        text: '女子'
    },
    {
        id: 'male',
        text: '男子'
    }
];

// 芯片厂商
iRunner.chipMakerConfig = [
    {
        id: 0,
        name: 'xiaomi'
    },
    {
        id: 1,
        name: 'active'
    },
    {
        id: 2,
        name: 'ct'
    },
    {
        id: 3,
        name: 'nfc'
    },
    {
        id: 4,
        name: 'beedancing'
    }
];

// 初始化赛事公司基本类型
iRunner.raceScaleConfig = [
    {
        id: 'organizer', 
        name: '主办机构'
    },
    {
        id: 'undertake', 
        name: '承办或运营方'
    },
    {
        id: 'facilitator', 
        name: '第三方服务商'
    },
    {
        id: 'club', 
        name: '社团等其他类型'
    }
];

// 赛事状态
iRunner.raceStatusConfig = [
    {
        value: 'notstart',
        text: '未开始'
    },
    {
        value: 'started',
        text: '进行中'
    },
    {
        value: 'ended',
        text: '已结束'
    }
];


// 初始化科目基本信息
iRunner.courseConfig = {
    trackTypes: [
        {
            id: 0, 
            name: '单程', 
            value: 'oneway'
        },
        {
            id: 1, 
            name: '绕圈', 
            value: 'circle'
        },
        // {
        //     id: 2, 
        //     name: '折返',
        //     value: 'retrace'
        // },
        {
            id: 3, 
            name: '自定义',
            value: 'custom'
        }
    ],
    raceTypes: [
        {
            id: 0, 
            name: '定时'
        },
        {
            id: 1, 
            name: '定距'
        }
    ],
    ranking: [
        {
            scope: '',
            value: 1,
            name: '个人'
        },
        {
            scope: 'ranking_man',
            value: 2,
            name: '个人男子'
        },
        {
            scope: 'ranking_woman',
            value: 4,
            name: '个人女子'
        },
        {
            scope: '',
            value: 8,
            name: '团体'
        },
        {
            scope: 'team_ranking_man',
            value: 16,
            name: '团体男子'
        },
        {
            scope: 'team_ranking_woman',
            value: 32,
            name: '团体女子'
        },
        {
            scope: 'ranking_team',
            value: 64,
            name: '团体混合'
        }
    ]
}

/**
 * 语言包入口
 * 读取语言包, 语言包定义于全局变量 L
 * 当data参数存在时，会调用模板
 * @method l
 * @param {String} langNS 语言包命名空间
 * @param {Object} data  模板数据，当data === true 时，调用 L 对象
 */
iRunner.l = function(langNS, data) {
    var _langNs = langNS, lang;
    if (typeof L == "undefined") {
        return langNS;
    }
    lang = L;
    langNS = (langNS || "").split(".");
    for (var i = 0, ns; ns = langNS[i++]; ) {
        lang = lang[ns];
        if (!lang) {
            break;
        }
    }
    return data ? (data === true ? $.template(lang, L) : $.template(lang, data)) : lang || _langNs;
}

/**
 * 全局设置，包括一些通用设置及通用插件默认配置
 */
iRunner.settings = {
    KEYCODE: {
        ENTER: 13,
        DELETE: 46,
        BACKSPACE: 8,
        UP: 38,
        DOWN: 40
    },
    imageTypes: "*.gif;*.jpg;*.jpeg;*.png;",
            dataTable: {

                /*基本参数设置，以下参数设置和默认效果一致*/
                "paginate": true, //翻页功能
                "lengthChange": true, //改变每页显示数据数量
                "filter": true, //过滤功能
                "sort": true, //排序功能
                "info": true,//页脚信息
                "autoWidth": true,//自动宽度
                /*默认排序设置*/
                // "aSorting": [[ 4, "desc" ]],//设置第5个元素为默认排序
                /*默认翻页样式设置*/
                "paginationType": "full_numbers",
                /*是否开启主题*/
                // "jQueryUI": true,
                /*语言设置*/
                "language": {
                    "lengthMenu": "每页显示 _MENU_条",
                    "zeroRecords": "没有找到符合条件的数据",
                    "processing": "加载中...",
                    "info": "当前第 _START_ - _END_ 条　共计 _TOTAL_ 条",
                    "infoEmpty": "",
                    // "infoFiltered": "(从 _MAX_ 条记录中过滤)",
                    "infoFiltered": "",
                    "search": "搜索：",
                    "paginate": {
                        "first": "首页",
                        "previous": "前一页",
                        "next": "后一页",
                        "last": "尾页"
                    }
                },

                // --- Data
                processing: true,
                serverSide: true,
                // --- Language
                // language: $.extend({}, iRunner.l("DATATABLE"), {
                //     processing: 'Loading...'
                // }),
                // --- Pagination
                pageLength: 50,
                pagingType: "full_numbers",
                lengthMenu: [10, 20, 50, 100, 200],
                // dom: "rt<'page-list-footer'lp>"
                dom: '<"page-list-footer"<br/>lp<"clear">irt<"page-list-footer"lp<"clear">ir'
            }
}

/**
 * [commonColumns description]
 */
iRunner.commonColumns = [
    {
        'className': 'details-control',
        'data': 'bib',
        'orderable': false
    },
    {
        'data': 'bib',
        'orderable': true,
        'render': function(data, type, row) {
            return '<a href="javascript:;" data-action="runner" data-id="' + row.runner_id + '">' + row.bib + '</a>';
        }
    },
    {
        'data': 'runner_name',
        'orderable': true,
        'render': function(data, type, row) {
            return iRunner.filter(row.runner_name)
        }
    },
    {
        'data': 'runner_gender',
        'orderable': true,
        'render': function(data, type, row) {
            return iRunner.gender(row.runner_gender)
        }
    },
    {
        'data': 'course_name',
        'orderable': true,
        'render': function(data, type, row) {
            return iRunner.filter(row.course_name)
        }
    },
    {
        'data': 'team_name',
        'orderable': true,
        'render': function(data, type, row) {
            return iRunner.filter(row.team_name)
        }
    },
    {
        'data': 'race_status',
        'orderable': true,
        'render': function(data, type, row) {
            return iRunner.runnerStatus(row.race_status);
        }
    },
    {
        'data': 'claimpack',
        'orderable': false,
        'render': function(data, type, row) {
            return row.claimpack == 'no' ? '未领取' : '已领取';

        }
    },
    {
        'data': '',
        'orderable': false,
        'render': function(data, type, row) {
            var yes = "<button type='button' class='btn btn-sm btn-info m-t-n-xs' style='margin-top: auto;' data-runnerid='" + row.runner_id + "' data-action='claimpack' data-bib='" + iRunner.filter(row.bib) + "'>设为已领取</button>";
            return row.claimpack != 'no' ? '--' : yes;

        }
    }
];

iRunner.runnerStatus = function(status) {
    switch(status)
    {
        case 'dns':
          return "未参赛";
          break;
        case 'dnf':
          return "未完赛";
          break;
        case 'finished':
          return "已完赛";
          break;
        case 'normal':
          return "正常";
          break;
        default:
          return "--";
    }
}

/**
 * [chipColumns description]
 */
iRunner.chipColumns = [
    {
        'data': 'chip_mac',
        'orderable': true,
        'render': function(data, type, row) {
            return '<a href="javascript:;" data-action="chip" data-id="' + row.id + '">' + row.chip_mac + '</a>';
        }
    },
    {
        'data': 'chip_maker',
        'orderable': true,
        'render': function(data, type, row) {
            return iRunner.filter(row.chip_maker)
        }
    },
    {
        'data': 'bib',
        'orderable': true,
        'render': function(data, type, row) {
            return iRunner.filter(row.bib)
        }
    },
    {
        'data': 'chip_no',
        'orderable': true,
        'render': function(data, type, row) {
            return iRunner.filter(row.chip_no)
        }
    }
];

/**
 * [scoreColumns description]
 */
iRunner.scoreColumns = [
    {
        'data': 'bib',
        'orderable': true,
        'render': function(data, type, row) {
            return iRunner.filter(row.bib)
        }
    },
    {
        'data': 'runner_name',
        'orderable': true,
        'render': function(data, type, row) {
            return iRunner.filter(row.runner_name)
        }
    },
    {
        'data': 'runner_gender',
        'orderable': true,
        'render': function(data, type, row) {
            return iRunner.gender(row.runner_gender)
        }
    },
    {
        'data': 'official_time',
        'orderable': true,
        'render': function(data, type, row) {
            return iRunner.filter(row.official_time)
        }
    },
    {
        'data': 'net_time',
        'orderable': true,
        'render': function(data, type, row) {
            return iRunner.filter(row.net_time)
        }
    },
    {
        'data': 'rank',
        'orderable': false,
        'render': function(data, type, row) {
            return iRunner.filter(row.rank)
        }
    }
];

/**
 * [rawdataColumns description]
 */
iRunner.rawdataColumns = [
    {
        'className': 'details-control',
        'data': 'bib',
        'orderable': false
    },
    {
        'data': 'bib',
        'orderable': false,
        'render': function(data, type, row) {
            return iRunner.filter(row.bib)
        }
    },
    {
        'data': 'runner_name',
        'orderable': false,
        'render': function(data, type, row) {
            return iRunner.filter(row.runner_name)
        }
    },
    {
        'data': 'sex',
        'orderable': false,
        'render': function(data, type, row) {
            return iRunner.gender(row.sex)
        }
    },
    {
        'data': 'cp_name',
        'orderable': false,
        'render': function(data, type, row) {
            return iRunner.filter(row.cp_name)
        }
    },
    {
        'data': 'chip_no',
        'orderable': false,
        'render': function(data, type, row) {
            return iRunner.filter(row.chip_no)
        }
    },
    {
        'data': 'time_record',
        'orderable': false,
        'render': function(data, type, row) {
            return iRunner.filter(row.time_record)
        }
    },
    {
        'data': 'import',
        'orderable': false,
        'render': function(data, type, row) {
            return row.import == 'system' ? '设备' : '人工';
        }
    },
    {
        'data': 'status',
        'orderable': false,
        'render': function(data, type, row) {
            return row.status == 'enable' ? '有效' : '无效';
        }
    },
    {
        'data': '',
        'orderable': false,
        'render': function(data, type, row) {
            var text = row.status == 'enable' ? '置为无效' : '置为有效'
                type = row.status == 'enable' ? 'disable' : 'enable';
                
            return '<button type="button" class="btn btn-sm btn-primary m-t-n-xs" data-action="status" data-type="' + type + '" data-id="' + row.record_id + '">' + text + '</button>';
        }
    }
];


/**
 * [personTableColumns description]
 */
iRunner.onewayColumns = [
    {
        'data': 'bib',
        'orderable': false,
        'render': function(data, type, row) {
            console.log(row); 
            return '<a data-action="realtime" data-id="' + row.runner_id + '" data-course="' + row.course_id +'">' + iRunner.filter(row.bib) + '</a>';
        }
    },
    {
        'data': 'runner_name',
        'orderable': false,
        'render': function(data, type, row) {
            return iRunner.filter(row.runner_name)
        }
    },
    {
        'data': 'cp_name',
        'orderable': false,
        'render': function(data, type, row) {
            return iRunner.filter(row.cp_name)
        }
    },
    {
        'data': 'arrive_time',
        'orderable': false,
        'render': function(data, type, row) {
            return iRunner.filter(row.arrive_time.substr(11))
        }
    },
    {
        'data': 'rank',
        'orderable': false,
        'render': function(data, type, row) {
            return iRunner.filter(row.rank)
        }
    },
    {
        'data': '',
        'orderable': false,
        'render': function(data, type, row) {
            return "<button type='button' class='btn btn-sm btn-primary m-t-n-xs' style='margin-top: auto;' data-scoreid='" + row.score_id + "' data-action='realtime_list' data-bib='" + iRunner.filter(row.bib) + "'>置无效</button>"
        }
    }
];


/**
 * [circleColumns description]
 */
iRunner.circleColumns = [
    {
        'data': 'bib',
        'orderable': false,
        'render': function(data, type, row) {
            return '<a href="javascript:;" data-action="realtime" data-id="' + row.runner_id + '" data-course="' + row.course_id +'">' + iRunner.filter(row.bib) + '</a>';
        }
    },
    {
        'data': 'runner_name',
        'orderable': false,
        'render': function(data, type, row) {
            return iRunner.filter(row.runner_name)
        }
    },
    {
        'data': 'use_time',
        'orderable': false,
        'render': function(data, type, row) {
            return iRunner.filter(row.use_time)
        }
    },
    {
        'data': 'circle_number',
        'orderable': false,
        'render': function(data, type, row) {
            return iRunner.filter(row.circle_number)
        }
    },
    {
        'data': 'rank',
        'orderable': false,
        'render': function(data, type, row) {
            return iRunner.filter(row.rank)
        }
    },
    {
        'data': '',
        'orderable': false,
        'render': function(data, type, row) {
            return "<button type='button' class='btn btn-sm btn-primary m-t-n-xs' style='margin-top: auto;' data-scoreid='" + row.score_id + "' data-action='realtime_list' data-bib='" + iRunner.filter(row.bib) + "'>置无效</button>"
        }
    }
];


/**
 * [personOnewayDistColumns 个人单程定距行模板]
 */
iRunner.personOnewayDistColumns = [
    {
        'data': 'bib',
        'orderable': false,
        'render': function(data, type, row) {
            return '<a href="javascript:;" data-action="realtime" data-runnerid="' + row.runner_id + '" data-course="' + row.course_id +'">' + iRunner.filter(row.bib) + '</a>';

        }
    },
    {
        'data': 'runner_name',
        'orderable': false,
        'render': function(data, type, row) {
            return iRunner.filter(row.runner_name)
        }
    },
    {
        'data': 'runner_gender',
        'orderable': false,
        'render': function(data, type, row) {
            return iRunner.filter(row.runner_gender)
        }
    },
    {
        'data': 'cp_name',
        'orderable': false,
        'render': function(data, type, row) {
            return iRunner.filter(row.cp_name)
        }
    },
    {
        'data': 'leave_time',
        'orderable': false,
        'render': function(data, type, row) {
            return iRunner.filter(row.leave_time)
        }
    },
    {
        'data': 'gunTime',
        'orderable': false,
        'render': function(data, type, row) {
            return iRunner.filter(row.gunTime)
        }
    },
    {
        'data': '',
        'orderable': false,
        'render': function(data, type, row) {
            return iRunner.filter(row.seq)
        }
    },
    {
        //漏点
        'data': '',
        'orderable': false,
        'render': function(data, type, row) {
            return ''
        }
    },
    {
        //选手有效性
        'data': '',
        'orderable': false,
        'render': function(data, type, row) {
            return "<button type='button' class='btn btn-sm btn-primary m-t-n-xs' style='margin-top: auto;' data-scoreid='" + row.score_id + "' data-action='realtime_list' data-bib='" + iRunner.filter(row.bib) + "'>置无效</button>"
        }
    }
];


/**
 * [personOnewayDistColumns 个人绕圈定距行模板]
 */
iRunner.personCircleDistColumns = [
    {
        'data': 'bib',
        'orderable': false,
        'render': function(data, type, row) {
            return '<a href="javascript:;" data-action="realtime" data-runnerid="' + row.runner_id + '" data-course="' + row.course_id +'">' + iRunner.filter(row.bib) + '</a>';
            // return iRunner.filter(row.bib)
        }
    },
    {
        'data': 'runner_name',
        'orderable': false,
        'render': function(data, type, row) {
            return iRunner.filter(row.runner_name)
        }
    },
    {
        'data': 'runner_gender',
        'orderable': false,
        'render': function(data, type, row) {
            return iRunner.filter(row.runner_gender)
        }
    },
    {
        'data': 'nums',
        'orderable': false,
        'render': function(data, type, row) {
            return '第' + row.nums + '圈';
        }
    },
    {
        'data': 'leave_time',
        'orderable': false,
        'render': function(data, type, row) {
            return iRunner.filter(row.leave_time)
        }
    },
    {
        'data': 'gunTime',
        'orderable': false,
        'render': function(data, type, row) {
            return iRunner.filter(row.gunTime)
        }
    },
    {
        'data': '',
        'orderable': false,
        'render': function(data, type, row) {
            return iRunner.filter(row.seq)
        }
    },
    {
        //漏点
        'data': '',
        'orderable': false,
        'render': function(data, type, row) {
            return ''
        }
    },
    {
        //选手有效性
        'data': '',
        'orderable': false,
        'render': function(data, type, row) {
            return "<button type='button' class='btn btn-sm btn-primary m-t-n-xs' style='margin-top: auto;' data-scoreid='" + row.score_id + "' data-action='realtime_list' data-bib='" + iRunner.filter(row.bib) + "'>置无效</button>"
        }
    }
];

/**
 * 短信发送日志模板
 */
iRunner.smsSendLog = [
    {
        'className': 'details-control',
        'data': 'log_id',
        'orderable': false
    },
    {
        'data': 'log_id',
        'orderable': false,
        'render': function(data, type, row) {
            return row.log_id
        }
    },
    {
        'data': 'name',
        'orderable': false,
        'render': function(data, type, row) {
            return row.name
        }
    },
    {
        'data': 'mobile',
        'orderable': false,
        'render': function(data, type, row) {
            return row.mobile
        }
    },
    {
        'data': 'content',
        'orderable': false,
        'render': function(data, type, row) {
            return row.content.length > 10 ? row.content.substr(0, 10) + '...' : row.content
        }
    },
    {
        'data': 'sent_time',
        'orderable': false,
        'render': function(data, type, row) {
            return row.sent_time
        }
    },
    {
        'data': 'sent_status',
        'orderable': false,
        'render': function(data, type, row) {
            return row.sent_status == 'success' ? "成功" : "失败"
        }
    }
];

/**
 * 选手号列表模板
 */
iRunner.bibColumns = [
    {
        'data': 'bib',
        'orderable': false,
        'render': function(data, type, row) {
            return iRunner.filter(row.bib)
        }
    },
    {
        'data': 'course',
        'orderable': false,
        'render': function(data, type, row) {
            return iRunner.filter(row.course)
        }
    },
    {
        'data': 'price',
        'orderable': false,
        'render': function(data, type, row) {
            return iRunner.filter(row.price)
        }
    }
];

/**
 * 已报名选手列表模板
 */
iRunner.regesterColumns = [
    {
        'className': 'details-control',
        'data': 'register_id',
        'orderable': false
    },
    {
        // 报名id
        'data': 'register_id',
        'orderable': false,
        'render': function(data, type, row) {
            return iRunner.filter(row.register_id)
        }
    },
    {
        // 科目
        'data': 'course',
        'orderable': false,
        'render': function(data, type, row) {
            return iRunner.filter(row.course)
        }
    },
    {
        // 姓名
        'data': 'passport_name',
        'orderable': false,
        'render': function(data, type, row) {
            return iRunner.filter(row.passport_name)
        }
    },
    {
        // 性别
        'data': 'gender',
        'orderable': false,
        'render': function(data, type, row) {
            return iRunner.filter(row.gender)
        }
    },
    {
        // 手机号
        'data': 'phone',
        'orderable': false,
        'render': function(data, type, row) {
            return iRunner.filter(row.phone)
        }
    },
    {
        // 证件号
        'data': 'identity',
        'orderable': false,
        'render': function(data, type, row) {
            return iRunner.filter(row.identity)
        }
    },
    {
        // 支付状态
        'data': '',
        'orderable': false,
        'render': function(data, type, row) {
            return iRunner.registerPayStatus(row.pay_status)
        }
    },
    {
        // 支付状态修改
        'data': '',
        'orderable': false,
        'render': function(data, type, row) {
            return iRunner.registerPayBtnStatus(row)
        }
    },
    {
        // 审核状态
        'data': 'status',
        'orderable': false,
        'render': function(data, type, row) {
            return iRunner.registerExamineStatus(row.status)
        }
    },
    {
        // 支付状态修改
        'data': '',
        'orderable': false,
        'render': function(data, type, row) {
            return iRunner.registerExamineBtnStatus(row)
        }
    },
    {
        // 支付状态修改
        'data': '',
        'orderable': false,
        'render': function(data, type, row) {
            return row.status == 'cancel' ? '-' : "<button type='button' class='btn btn-sm btn-primary m-t-n-xs' style='margin-top: auto;' data-action='cancel' data-registerid='" + row.register_id + "' data-name='" + row.passport_name + "' data-paystatus='" + row.pay_status + "' >取消报名</button>";
        }
    }
];



export default iRunner;
